package com.cennavi.minenavidemo.activity.geocode;

import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.minedata.minenavi.map.Marker;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.GeocodeResult;
import com.minedata.minenavi.poiquery.GeocodeSearch;
import com.minedata.minenavi.poiquery.LatLonPoint;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.RegeocodeResult;
import com.minedata.minenavi.poiquery.SearchErrorCode;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 逆地理编码搜索
 */
public class RegeocodeActivity extends AppCompatActivity implements MapPresenter.OnMapViewListener,
        PoiPresenter.OnPoiSearchListener {

    private MineMap mMineMap;
    public Marker marker;
    private GeocodeSearch geocoderSearch;
    private MapPresenter mMapPresenter;
    private PoiPresenter mPoiPresenter;

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    @BindView(R.id.etLat)
    EditText etLat;//纬度
    @BindView(R.id.etLon)
    EditText etLon;//经度
    @BindView(R.id.result)
    TextView result;
    @BindView(R.id.start)
    Button btnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regeocode);
        mUnbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(RegeocodeActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(this);
        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(RegeocodeActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initRegeocode();
        geocoderSearch = new GeocodeSearch(this);
        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int resultID) {

            }

            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int resultID) {
                if (resultID == SearchErrorCode.none) {
                    List<RegeocodeAddress> regeocodeAddresses = regeocodeResult.getRegeocodeAddressList();
                    List<PoiItem> poiItems = regeocodeAddresses.get(0).getPois();

                    if (poiItems != null && poiItems.size() > 0) {


                    }
                }
            }
        });
    }

    /**
     * 逆地理编码查询
     */
    @OnClick(R.id.start)
    public void reGeocode() {
        if (TextUtils.isEmpty(etLat.getText().toString())) {
            Toast.makeText(this, "请输入纬度", Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(etLon.getText().toString())) {
            Toast.makeText(this, "请输入经度", Toast.LENGTH_LONG).show();
            return;
        }
        double lat = Double.parseDouble(etLat.getText().toString().trim());
        double lon = Double.parseDouble(etLon.getText().toString().trim());

        mPoiPresenter.reGeocode(new LatLonPoint(lat, lon));
    }

    private void showResult(String data) {
        result.setText(data);
        result.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {

    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {
        List<PoiItem> poiItems = regeocodeAddress.getPois();

        if (poiItems != null && poiItems.size() > 0) {
            PoiItem poiItem = poiItems.get(0);

            StringBuffer sb = new StringBuffer();
            sb.append("geo省:").append(regeocodeAddress.getProvince()).append("\n");
            sb.append("geo城市:").append(regeocodeAddress.getCity()).append("\n");
            sb.append("geo区县:").append(regeocodeAddress.getDistrict()).append("\n");
            sb.append("geo所在点名称:").append(poiItem.title).append("\n");
            sb.append("geo所在地址:").append(poiItem.snippet).append("\n");
            sb.append("geo坐标:").append(poiItem.location.getLatitude() + "," + poiItem.location.getLongitude()).append("\n");
            sb.append("geo方向:").append(poiItem.direction).append("\n");
            sb.append("geo距离:").append(poiItem.distance);
            showResult(sb.toString());

            Point point = Tools.latLngToPoint(new LatLng(poiItem.location.getLatitude(), poiItem.location.getLongitude()));
            mMineMap.setPointToCenter(point.x, point.y);

            mMapPresenter.deletePoiMarker();
            LatLng latLng = new LatLng(poiItem.location.getLatitude(), poiItem.location.getLongitude());
            mMapPresenter.addPoiMarker(latLng);
        }
    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {

    }

    @Override
    public void onGetInputtips(List<Tip> tips) {

    }

    @Override
    public void onMapReady() {
        mMineMap = mMapPresenter.getMineMap();
        Point point = Tools.latLngToPoint(new LatLng(39.984059, 116.307771));
        mMineMap.setPointToCenter(point.x, point.y);
        mMineMap.setZoomLevel(11);
    }

    @Override
    public void onswitchLocation(int imageLevel) {

    }
}
